package com.yksj.healthtalk.comm;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.yksj.healthtalk.ui.R;

public class SalonFileBuyDialog extends DialogFragment {
	String mPrice;
	TextView priceTextV;
	OnClickListener mClickListener;
	FragmentManager mFragmentManager;
	
	/**
	 * @param manager
	 * @param content 文字内容
	 */
	public static void showDialog(FragmentManager manager,String price,OnClickListener clickListener){
		Fragment fragment = manager.findFragmentByTag("SalonFileBuyDialog");
		FragmentTransaction ft = manager.beginTransaction();
		if(fragment != null){
			ft.remove(fragment);
		}
		DialogFragment dialog = new SalonFileBuyDialog(price,clickListener,manager);
		ft.add(dialog,"SalonFileBuyDialog");
		ft.commitAllowingStateLoss();
	}
	
	public static void dismiss(FragmentManager fragmentManager){
		if(fragmentManager == null)return;
		SalonFileBuyDialog fragment = (SalonFileBuyDialog)fragmentManager.findFragmentByTag("SalonFileBuyDialog");
		if(fragment != null){
			fragment.dismissAllowingStateLoss();
		}
	}
	
	public SalonFileBuyDialog(String price,OnClickListener clickListener,FragmentManager fragmentManager) {
		mPrice = price;
		mFragmentManager = fragmentManager;
		mClickListener = clickListener;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity(),R.style.translucent_dialog);
		dialog.setContentView(R.layout.chat_document_buy_dialog_layout);
		priceTextV = (TextView)dialog.findViewById(R.id.price);
		dialog.findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss(mFragmentManager);
			}
		});
		dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mClickListener != null)mClickListener.onClick(v);
				dismiss(mFragmentManager);
			}
		});
		String priceStr = "该文件下载需要付费:%1$s";
		priceStr = String.format(priceStr,mPrice);
		priceTextV.setText(priceStr);
		return dialog;
	}
}
